package cn.lyjuan.android.demofive.frame.tpl;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by chad on 12/11/2017.
 */
public abstract class RecyclerViewFragmentImpl extends FragmentImpl
{
    /**
     * RecyclerView控件
     */
    protected RecyclerView rv;

    protected IListViewModel viewModel;

    protected RecyclerView.Adapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(layoutId(), container, false);

        rv = (RecyclerView) view.findViewById(recyclerViewId());

        loadViewModel();

        recyclerViewSetting(view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        loadData();
    }

    /**
     * 加载数据
     */
    protected void loadData()
    {
        viewModel.loadData(true);// 初始加载数据
    }

    /**
     * RecyclerView设置
     *
     * @param view fragment主内容视图
     */
    protected void recyclerViewSetting(View view)
    {
        rv.setAdapter(adapter);
    }

    /**
     * 获取数据处理模型
     *
     * @return
     */
    protected abstract void loadViewModel();

    /**
     * RecyclerView id
     *
     * @return
     */
    protected abstract int recyclerViewId();
}